package com.myapplication.zhou.alldemo;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cocosw.bottomsheet.BottomSheet;
import com.myapplication.callback.LoginCallback;
import com.myapplication.dialog.LoadingDialog;
import com.myapplication.helper.PreferencesHelper;
import com.myapplication.helper.PreferencesHelper1;
import com.myapplication.interfaces.LoginHttpConnect;
import com.myapplication.model.UserInfo;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener, LoginCallback {

    @ViewInject(R.id.toolbar)
    private Toolbar toolbar;
    @ViewInject(R.id.button_login)
    private Button button_login;
    private EditText editText_username, editText_password;
    private TextView textView_register, textView_forget;
    private String username, userpassword;
    private LoginHttpConnect loginHttpConnect;
    @ViewInject(R.id.textView_title)
    private TextView textView_title;
    private AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        x.view().inject(this);
        initView();
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        textView_title.setText("登陆商城");
        loginHttpConnect = new LoginHttpConnect();
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        button_login.setOnClickListener(this);
        textView_forget.setOnClickListener(this);
        textView_register.setOnClickListener(this);
    }

    /**
     * 判断输入框是否为空
     *
     * @return boolean
     */
    public boolean checkoutnullable() {
        boolean b = true;
        username = editText_username.getText().toString().trim();
        userpassword = editText_password.getText().toString().trim();
        if (username.equals("")) {
            editText_username.setError("用户名不能为空");
            b = false;
        }
        if (userpassword.equals("")) {
            editText_username.setError("密码不能为空");
            b = false;
        }
        return b;
    }

    public void initView() {
        editText_password = (EditText) findViewById(R.id.editText_userpassword);
        editText_username = (EditText) findViewById(R.id.editText_username);
        textView_forget = (TextView) findViewById(R.id.textView_forget);
        textView_register = (TextView) findViewById(R.id.textView_register);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_login:
                if (checkoutnullable()) {
                    Log.i("username>>>>", username);
                    Log.i("userpassword>>>>", userpassword);
                    loginHttpConnect.login(username, userpassword, this);
                    alertDialog = LoadingDialog.showDialog(this, "正在登陆......").create();
                    alertDialog.show();
                }
                break;
            case R.id.textView_forget:
                new BottomSheet.Builder(this).title("忘记密码RECEIVE").sheet(R.menu.buttom_sheet_forget_password).listener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.menu_forget:
                                Intent intent = new Intent();
                                intent.setClass(LoginActivity.this, ForgetPasswordActivity.class);
                                startActivity(intent);
                                break;
                            case R.id.cancle:
                                break;
                        }
                        return false;

                    }
                }).show();
                break;
        }
    }

    private String getSH() {
        return PreferencesHelper1.getSharedValue(this, "Login", "Tag");
    }

    /**
     * 登陆成功回调接口
     */
    @Override
    public void onLoginCallbackSuccess(String onLoginSuccess) {
        Log.e("_____onLoginSuccess", onLoginSuccess);
        try {
            JSONObject jsonObject = new JSONObject(onLoginSuccess);
            int code = jsonObject.optInt("code");
            if (code == 200) {
                JSONObject jsonObject1 = jsonObject.optJSONObject("datas");
                String key = jsonObject1.optString("key");
                UserInfo userInfo = new UserInfo();
                userInfo.setKey(key);
                userInfo.setPassword(userpassword);
                userInfo.setUsername(username);
                PreferencesHelper.writetoPreference(this, userInfo);
                PreferencesHelper1.setSharedValue(this, "Login", "Tag", "success");
                alertDialog.dismiss();
                finish();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLoginCallbackFail(String onLoginFail) {
        alertDialog.dismiss();
    }
}
